Sorts an array using default and custom comparer

The following example shows how to sort the values in an array using the default comparer and a custom comparer that reverses the sort order.

C# .NET

public class myReverserClass : IComparer
{

         // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
         int IComparer.Compare( Object x, Object y )
         {
                  return( (new CaseInsensitiveComparer()).Compare( y, x ) );
         }
}

public static String DoTest()
{

         string strRet = "";
         // Creates and initializes a new Array and a new custom comparer.
         String[] myArr = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog" };
         IComparer myComparer = new myReverserClass();

         // Displays the values of the Array.
         strRet += ( "The Array initially contains the following values:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts a section of the Array using the default comparer.
         Array.Sort( myArr, 1, 3 );
         strRet += ( "After sorting a section of the Array using the default comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts a section of the Array using the reverse case-insensitive comparer.
         Array.Sort( myArr, 1, 3, myComparer );
         strRet += ( "After sorting a section of the Array using the reverse case-insensitive comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts the entire Array using the default comparer.
         Array.Sort( myArr );
         strRet += ("After sorting the entire Array using the default comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts the entire Array using the reverse case-insensitive comparer.
         Array.Sort( myArr, myComparer );
         strRet += ( "After sorting the entire Array using the reverse case-insensitive comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         return strRet;
         
}

public static string PrintIndexAndValues( String[] myArr )
{
         string strRet = "";
         for ( int i = 0; i < myArr.Length; i++ )
         {
                  strRet += String.Format(" [{0}] : {1}", i, myArr[i] );
                  strRet += "\r\n";
         }
         strRet += "\r\n";
         return strRet;
}



 

Blaze++ .NET


class myReverserClass : public IComparer
{
public:
         // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
         int IComparer::Compare( Object& x, Object& y )
         {
                  CaseInsensitiveComparer cic;
                  return cic.Compare( y, x );
         }
};

static String DoTest()
{

         String strRet = "";
         // Creates and initializes a new Array and a new custom comparer.
         String arr[] = { "The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"};
         Array myArr(arr, 9);
         myReverserClass myComparer;

         // Displays the values of the Array.
         strRet += ( "The Array initially contains the following values:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts a section of the Array using the default comparer.
         Array::Sort( myArr, 1, 3 );
         strRet += ( "After sorting a section of the Array using the default comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts a section of the Array using the reverse case-insensitive comparer.
         Array::Sort( myArr, 1, 3, myComparer );
         strRet += ( "After sorting a section of the Array using the reverse case-insensitive comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts the entire Array using the default comparer.
         Array::Sort( myArr );
         strRet += ("After sorting the entire Array using the default comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         // Sorts the entire Array using the reverse case-insensitive comparer.
         Array::Sort( myArr, myComparer );
         strRet += ( "After sorting the entire Array using the reverse case-insensitive comparer:\r\n" );
         strRet += PrintIndexAndValues( myArr );

         return strRet;
         
}

static String PrintIndexAndValues( Array& myArr )
{
         String strRet = "";
         for ( int i = 0; i < myArr.Length; i++ )
         {
                  strRet += String::Format(" [{0}] : {1}", i, myArr[i] );
                  strRet += "\r\n";
         }
         strRet += "\r\n";
         return strRet;
}